/*
 * Mochilas, bolsos, etc. Echos para contener otros items.
 *
 * This is part of the game The Rooms
 * Copyright (C) 2010  Elvio Sarquis
 *
 * Get the GNU GPL v3 in http://www.gnu.org/licenses/
 */

package rooms.items;

/**
 *
 * @author dask
 */
public class Container extends Item{

    private Item[] contains;
    private int ncontains;
    private int ncontainsmax;
    private int containsweight;
    private int maxweight;

    public Container(String name, String description, int x, int y, int weight, int price, int color, char symbol, int ncontainsmax, int maxweight){

        super(name, description, x, y, weight, price, color, symbol);

        ncontains = 0;
        containsweight = 0;

        this.ncontainsmax = ncontainsmax;
        this.maxweight = maxweight;
    }

    public Container(Container container){

        super((Item) container);

        ncontains = 0;
        containsweight = 0;
        
        this.ncontainsmax = container.ncontainsmax;
        this.maxweight = container.maxweight;
    }

    public boolean addItem(Item item){

        if(ncontains>=ncontainsmax) return false;
        if(containsweight+item.getWeight() > maxweight) return false;

        ncontains++;
        containsweight += item.getWeight();

        Item[] temp = new Item[ncontains];

        for(int i = 0; i < contains.length; i++)
            temp[i] = contains[i];

        contains = temp;
        contains[ncontains-1] = item;
        
        return true;
    }

    public Item[] getItemList(){
        return contains;
    }

    public void removeItem(int index){

        ncontains--;
        containsweight -= contains[index].getWeight();

        Item[] tempo = new Item[contains.length - 1];

        for(int i = 0 ; i < contains.length ; i++) {

            if(i < index){
                tempo[i] = contains[i];
            }else if(i > index){
                tempo[i-1] = contains[i];
            }
        }
        contains = new Item[tempo.length];

        for(int i = 0 ; i < tempo.length ; i++) {
            contains[i] = tempo[i];
        }
    }

// <editor-fold defaultstate="collapsed" desc="Getters">
    public int getNContains() {
        return ncontains;
    }
    public int getNContainsMax() {
        return ncontainsmax;
    }
    public int getMaxWeight() {
        return maxweight;
    }
    public int getContentWeight(){
        return containsweight;
    }
    @Override
    public int getWeight(){
        return containsweight + this.getWeight();
    }// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Setters">
    public void setNContainsMax(int ncontainsmax) {
        this.ncontainsmax = ncontainsmax;
    }
    public void getMaxWeight(int maxweight) {
        this.maxweight = maxweight;
    }// </editor-fold>
}